package com.design_patterns.proxy.v2;

import java.util.Random;

/**
 * @author: 李德才
 * @description:
 * @create: 2020-08-30 18:53
 **/

/**
 * 将代理对象扩大，可以直接嵌套类调用
 */
public class Main2 {
    public static void main(String[] args) {
        /**
         * BrushTime().drawing() 中调用了传入对象的 .drawing() 方法
         *  当方法被执行，首先记录开开始时间，然后调用 BrushLog() 的  drawing() 方法
         *  BrushLog() 的  drawing() 又调用了传入参数的 drawing() 方法，所以输出了 before Drawing 后  Brush().drawing() 被执行
         */
        new BrushTime(new BrushLog(new Brush())).drawing();
    }
}

class Brush implements Drawing {

    @Override
    public void drawing() {
        System.err.println("画笔开始画画");
        try {
            Thread.sleep(new Random().nextInt(5000));
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.err.println("画笔绘画完成");
    }
}

class BrushTime implements Drawing {
    Drawing drawing;

    public BrushTime(Drawing drawing) {
        this.drawing = drawing;
    }

    @Override
    public void drawing() {
        long currentTimeMillis = System.currentTimeMillis();
        drawing.drawing();
        System.err.println("用时：" + (System.currentTimeMillis() - currentTimeMillis));
    }
}

class BrushLog implements Drawing {
    Drawing drawing;

    public BrushLog(Drawing drawing) {
        this.drawing = drawing;
    }

    @Override
    public void drawing() {
        System.err.println(" before Drawing ");
        drawing.drawing();
        System.err.println(" after Drawing ");
    }
}


interface Drawing {
    void drawing();
}